package com.atguigu.cloud.sevice.Impl;/**
 * @Auter zzh
 * @Date 2024/11/13
 */

import com.atguigu.cloud.entities.Merchant;
import com.atguigu.cloud.entities.Order;
import com.atguigu.cloud.entities.Product;
import com.atguigu.cloud.tool.load.page.PaginationResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;
import com.atguigu.cloud.mapper.ProductMapper;
import com.atguigu.cloud.sevice.ProductService;

import java.util.Collections;
import java.util.List;

/**
 * @projectName: keshe
 * @package: com.atguigu.cloud.sevice.Impl
 * @className: productService
 * @author: Eric
 * @description: TODO
 * @date: 2024/11/13 18:00
 * @version: 1.0
 */
@Service
public class ProductServiceImpl implements ProductService {
    @Resource
    ProductMapper productMapper;

    @Override
    public Product getById(Integer id) {

        QueryWrapper<Product> queryWrapper= new QueryWrapper<>();
        queryWrapper.eq("proid",id);
        Product product = productMapper.selectOne(queryWrapper);
        return product;

    }

    @Override
    public List<Product> getAll() {
        QueryWrapper<Product> queryWrapper= new QueryWrapper<>();
        List<Product> list_product = productMapper.selectList(queryWrapper);
        return list_product;

    }

    @Override
    public int add(Product product) {
        return productMapper.insert(product);

    }

    @Override
    public int delete(Integer id) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("proid",id);
        int result_id =productMapper.delete(queryWrapper);
        return result_id;
    }

    @Override
    public int update(Product product) {

        int id = product.getProid();
        UpdateWrapper<Product> updateWrapper = new UpdateWrapper<>();
        updateWrapper.eq("proid",id);
        productMapper.update(product,updateWrapper);
        return id;
    }

    //按照价格区间搜索商品
    @Override
    public List<Product> getByPrice(Integer min, Integer max) {
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if(min == null ) min = 0;
        if(max == null) max = 100000;
        queryWrapper.between("price", min, max);
        List<Product> list_product = productMapper.selectList(queryWrapper);
        return list_product;
    }
    @Override
    //模糊查询
    public List<Product> getByLike(String proname){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("proname",proname);
        List<Product> list_product = productMapper.selectList(queryWrapper);
        return list_product;
    }
    //按照类别搜索商品
    @Override
    public List<Product> getByDistribution(String category){
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category",category);
        List<Product> list_product = productMapper.selectList(queryWrapper);
        return list_product;
    }

    //分页
    //查询所有商品
    @Override
    public PaginationResult<Product> getPaginationAll(Integer currentPage) {
        //为0排除
        if (currentPage==0){
            throw new RuntimeException("输入目标页号为0,这将不被允许");
        }
        int pageSize = 3;
        // 创建分页对象
        Page<Product> page = new Page<>(currentPage, pageSize);

        // 创建查询条件构造器
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        long totalCount = productMapper.selectCount(queryWrapper);
        List<Product> list_product = productMapper.selectPage(page, queryWrapper).getRecords();

        // 如果总记录数为0，那么说明没数据就不用下一步查询了，提高效率。
        if (totalCount == 0){
            // 返回一个查询结果集，返回当前页、每页记录数、以及一个空的结果集
            return new PaginationResult<Product>(currentPage,pageSize,totalCount, Collections.EMPTY_LIST);
        }

        // 返回一个查询结果集，返回当前页、每页记录数、以及结果集
        return new PaginationResult<Product>(currentPage,pageSize,totalCount,list_product );


    }
    //按照价格区间搜索商品
    @Override
    public PaginationResult<Product> getPaginationByPrice(Integer min, Integer max,Integer currentPage) {
        //为0排除
        if (currentPage==0){
            throw new RuntimeException("输入目标页号为0,这将不被允许");
        }
        int pageSize = 3;
        // 创建分页对象
        Page<Product> page = new Page<>(currentPage, pageSize);

        // 创建查询条件构造器
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        if(min == null ) min = 0;
        if(max == null) max = 100000;
        long totalCount = productMapper.selectCount(queryWrapper);
        queryWrapper.between("price", min, max);
        List<Product> list_product = productMapper.selectPage(page, queryWrapper).getRecords();

        // 如果总记录数为0，那么说明没数据就不用下一步查询了，提高效率。
        if (totalCount == 0){
            // 返回一个查询结果集，返回当前页、每页记录数、以及一个空的结果集
            return new PaginationResult<Product>(currentPage,pageSize,totalCount, Collections.EMPTY_LIST);
        }

        // 返回一个查询结果集，返回当前页、每页记录数、以及结果集
        return new PaginationResult<Product>(currentPage,pageSize,totalCount,list_product );

    }
    @Override
    //模糊查询
    public PaginationResult<Product> getPaginationByLike(String proname,Integer currentPage){
        //为0排除
        if (currentPage==0){
            throw new RuntimeException("输入目标页号为0,这将不被允许");
        }
        int pageSize = 3;
        // 创建分页对象
        Page<Product> page = new Page<>(currentPage, pageSize);

        // 创建查询条件构造器
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        long totalCount = productMapper.selectCount(queryWrapper);
        queryWrapper.like("proname",proname);
        List<Product> list_product = productMapper.selectPage(page, queryWrapper).getRecords();

        // 如果总记录数为0，那么说明没数据就不用下一步查询了，提高效率。
        if (totalCount == 0){
            // 返回一个查询结果集，返回当前页、每页记录数、以及一个空的结果集
            return new PaginationResult<Product>(currentPage,pageSize,totalCount, Collections.EMPTY_LIST);
        }

        // 返回一个查询结果集，返回当前页、每页记录数、以及结果集
        return new PaginationResult<Product>(currentPage,pageSize,totalCount,list_product );
    }
    //按照类别搜索商品
    @Override
    public PaginationResult<Product> getPaginationByDistribution(String category,Integer currentPage){

        //为0排除
        if (currentPage==0){
            throw new RuntimeException("输入目标页号为0,这将不被允许");
        }
        int pageSize = 3;
        // 创建分页对象
        Page<Product> page = new Page<>(currentPage, pageSize);

        // 创建查询条件构造器
        QueryWrapper<Product> queryWrapper = new QueryWrapper<>();
        long totalCount = productMapper.selectCount(queryWrapper);
        queryWrapper.eq("category",category);
        List<Product> list_product = productMapper.selectPage(page, queryWrapper).getRecords();

        // 如果总记录数为0，那么说明没数据就不用下一步查询了，提高效率。
        if (totalCount == 0){
            // 返回一个查询结果集，返回当前页、每页记录数、以及一个空的结果集
            return new PaginationResult<Product>(currentPage,pageSize,totalCount, Collections.EMPTY_LIST);
        }

        // 返回一个查询结果集，返回当前页、每页记录数、以及结果集
        return new PaginationResult<Product>(currentPage,pageSize,totalCount,list_product );

    }

}
